$(document).ready(function() {
	var Main = {
		data() {
			return {
				mobile: '',
				vcode: '',
				password: ''
			}
		},
		methods: {
			getCode: function() {
				var self = this
				var mobile = $.trim(self.mobile)
				$.ajax({
					type: "post",
					url: "http://www.ibabypet.com.cn/public/index.php/api/account/sms/",
					async: true,
					data: {
						mobile: mobile
					},
					success: function(res) {
						res = JSON.parse(res);
						if(res.code == 200) {
							self.$message(res.msg)
							var countdown = 60;

							function sendemail() {
								var obj = $("#btn");
								settime(obj);
							}

							function settime(obj) { //发送验证码倒计时
								if(countdown == 0) {
									obj.attr('disabled', false);
									//obj.removeattr("disabled");
									obj.val("免费获取验证码");
									countdown = 60;
									return;
								} else {
									obj.attr('disabled', true);
									obj.val("重新发送(" + countdown + ")");
									countdown--;
								}
								setTimeout(function() {
									settime(obj)
								}, 1000)
							}
							sendemail()
						} else {
							self.$message(res.msg)
						}
					}
				})
			},
			registerBtn: function() {
				var self = this
				var mobile = $.trim(self.mobile);
				var password = $.trim(self.password);
				var vcode = $.trim(self.vcode);

				$.ajax({
					type: "post",
					url: "http://www.ibabypet.com.cn/public/index.php/api/account/registered/",
					async: true,
					data: {
						mobile: mobile,
						password: password,
						vcode: vcode
					},
					success: function(res) {
						res = JSON.parse(res);
						if(res.code == 200) {
							self.$message(res.msg)
							setTimeout(function() {
								window.location.href = 'http://www.ibabypet.com.cn/pcpet/login.html'
							}, 1500)
						} else {
							self.$message(res.msg)
						}
					}
				})
			}
		}
	}
	var Ctor = Vue.extend(Main)
	new Ctor().$mount('.register_content')
})
var Register = {
	url_api: 'http://www.ibabypet.com.cn/public/index.php/api/account/sms/', //获取手机验证码 mobile(注册手机号)
	url_api2: 'http://www.ibabypet.com.cn/public/index.php/api/account/registered/', //username(账号即手机号),password(密码),vcode(验证码)
	//输入手机号获取手机验证码
	getIdentifyingCode: function() {
		var that = this;
		var mobile_num, uspassworder, verifycode;
		$('.verify_btn').on('click', function() {
			mobile_num = $.trim($('#user').val());
			console.log('mobile_num', mobile_num)
			$.ajax({
				type: "post",
				url: that.url_api,
				async: true,
				data: {
					mobile: mobile_num
				},
				success: function(res) {
					res = JSON.parse(res);
					console.log('获取验证码返回res', res)
					if(res.code == 200) {
						alert('获取验证码成功！')
					}
				}
			});
		});
		//注册
		$('.register').on('click', function() {
			mobile_num = $.trim($('#user').val());
			uspassworder = $.trim($('#uspassworder').val());
			verifycode = $.trim($('#verifycode').val());

			console.log(mobile_num, verifycode, verifycode)
			$.ajax({
				type: "post",
				url: that.url_api2,
				async: true,
				data: {
					username: mobile_num,
					password: uspassworder,
					vcode: verifycode
				},
				success: function(res) {
					res = JSON.parse(res);
					console.log('注册返回数据res', res)
					if(res.code == 200) {
						alert('注册成功！');
						window.location.href = 'http://www.ibabypet.com.cn/pcpet/login.html';
					}
				}
			});
		});
	},

}